package com.zhiche.lisa.util;

import com.zhiche.lisa.oms.dao.model.OrderItem;
import com.zhiche.lisa.oms.service.ILocationService;
import com.zhiche.lisa.oms.service.IOrderItemService;
import com.zhiche.lisa.oms.vo.LocationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class OrderUtil {

    @Autowired
    IOrderItemService orderItemService;

    @Autowired
    ILocationService locationService;

//    @Value("${lisa.oms.getLocationByIdUrl}")
//    private String getLocationByIdUrl;
//
//    @Value("${lisa.oms.getOrderItemByIdUrl}")
//    private String getOrderItemByIdUrl;
//
//    @Value("${lisa.oms.getDriverLocationById}")
//    private String getDriverLocationById;

    //连接超时时间
    @Value("${lisa.socketTimeout}")
    private int socketTimeout;

    /**
     * 根据id得到订单明细
     * @param id
     * @return
     */
    public OrderItem getOrderItemById(String authHeader, Long id) {
        OrderItem orderItem = orderItemService.selectById(id);
//        OrderItemVo OrderItemVo = null;
//        try {
//            List<NameValuePair> headNamePairs = Lists.newArrayList();
//            headNamePairs.add(new BasicNameValuePair("Authorization", authHeader));
//            List<NameValuePair> params = Lists.newArrayList();
//            params.add(new BasicNameValuePair("id", id.toString()));
//            String strReturn = HttpClientUtil.get(getOrderItemByIdUrl, headNamePairs, params, socketTimeout);
//            if (StringUtils.isNotEmpty(strReturn)) {
//                RestfulResponse<OrderItemVo> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<OrderItemVo>>() {
//                });
//                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
//                    OrderItemVo = restfulResponse.getData();
//                }
//            }
//        } catch (Exception ex) {
//            throw new BaseException(ex.getMessage());
//        }
        return orderItem;
    }

    /**
     * 根据ID获取位置
     *
     * @param id
     * @return
     */
    public LocationVo getLocationVoById(String authHeader, Long id) {

        LocationVo locationVo = locationService.getLocationById(id);
//        LocationVo LocationVo = null;
//        try {
//            List<NameValuePair> headNamePairs = Lists.newArrayList();
//            headNamePairs.add(new BasicNameValuePair("Authorization", authHeader));
//            List<NameValuePair> params = Lists.newArrayList();
//            params.add(new BasicNameValuePair("id", id.toString()));
//            String strReturn = HttpClientUtil.get(getLocationByIdUrl, headNamePairs, params, socketTimeout);
//            if (!StringUtils.isEmpty(strReturn)) {
//                RestfulResponse<LocationVo> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LocationVo>>() {
//                });
//                if (Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())) {
//                    LocationVo = restfulResponse.getData();
//                }
//            }
//        } catch (Exception ex) {
//            throw new BaseException(ex.getMessage());
//        }
        return locationVo;
    }

    /**
     * 司机根据ID获取位置
     * @param id
     * @return
     */
    public LocationVo getDriverLocationVoById(String authHeader, Long id) {
        LocationVo locationVo = locationService.getDriverLocationById(id);
//        LocationVo LocationVo = null;
//        try{
//            List<NameValuePair> headNamePairs = Lists.newArrayList();
//            headNamePairs.add(new BasicNameValuePair("Authorization",authHeader));
//            List<NameValuePair> params = Lists.newArrayList();
//            params.add(new BasicNameValuePair("id",id.toString()));
//            String strReturn = HttpClientUtil.get(getDriverLocationById, headNamePairs, params, socketTimeout);
//            if(!StringUtils.isEmpty(strReturn)){
//                RestfulResponse<LocationVo> restfulResponse = JSON.parseObject(strReturn, new TypeReference<RestfulResponse<LocationVo>>(){});
//                if(Objects.nonNull(restfulResponse) && Objects.nonNull(restfulResponse.getData())){
//                    LocationVo= restfulResponse.getData();
//                }
//            }
//        }
//        catch (Exception ex){
//            throw new BaseException(ex.getMessage());
//        }
        return locationVo;
    }
}
